﻿using FlowDesigner.ExchangeDialog;
using FlowDesigner.HelperVision;
using FlowDesigner.Models;
using FlowDesigner.View;
using FlowDesigner.ViewModel;
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Shapes;
using Unity;

namespace FlowDesigner.Flow
{
    public class NodeS : Control, IDisposable
    {
        private bool IsDrop {  get; set; }=false;
        public Point staPoint { get; set; }
        private Canvas Canvas_ { get; set; }
        private NodeS nodeS;
        public NodeS()
        {
            this.MouseMove += NodeS_MouseMove;
            this.MouseLeftButtonDown += NodeS_MouseLeftButtonDown;
            this.MouseLeftButtonUp += NodeS_MouseLeftButtonUp;
            this.MouseLeave += NodeS_MouseLeave;
            //this.MouseRightButtonDown += NodeS_MouseDoubleClick;
        }
        /// <summary>
        /// 鼠标右键
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        //public void NodeS_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        //{
        //    var node = sender as NodeS;
        //    if (node != null)
        //    {
        //        node.ContextMenu=new ContextMenu();
        //    }

        //    // 获取 MainViewModel 实例
        //    BaseDialogViewModel mainViewModel = Containerfot.Container.container.Resolve<BaseDialogViewModel>();
        //    mainViewModel.ShowDialog(typeof(EditflownodeView), typeof(EditflownodeViewModel));
        //    var data = VisuaXml.FindVisualParent<Canvas>(sender as NodeS);
        //    IsDrop = false;
        //}

        public void NodeS_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            IsDrop = false;
        }

        public void NodeS_MouseLeftButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            IsDrop = false;
        }

        public void NodeS_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            IsDrop=true;
            Canvas_= VisuaXml.FindVisualParent<Canvas>(sender as NodeS);
        }

        public void NodeS_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
        {
            e.Handled = true;//阻止事件传递
            if(nodeS==null)
            nodeS = sender as NodeS;
            if (IsDrop)
            {
                if (nodeS.ParentItemsControl.NexflowIndex != 0)
                {
                    if(nodeS.ParentItemsControl.Tag=="结束")
                    IsDrop=false;
                    return;
                }
                //tdcont.MeasureOverride(new Size(double.PositiveInfinity, double.PositiveInfinity));
                Canvas.SetLeft(nodeS, e.GetPosition(Canvas_).X-20); // 水平位置
                Canvas.SetTop(nodeS, e.GetPosition(Canvas_).Y-20);  // 垂直位置
            }
            
        }

        static NodeS()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(NodeS), new FrameworkPropertyMetadata(typeof(NodeS)));
        }
        public string strFont
        {
            get { return (string)GetValue(strFontProperty); }
            set { SetValue(strFontProperty, value); }
        }

        // Using a DependencyProperty as the backing store for strFont.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty strFontProperty =
            DependencyProperty.Register("strFont", typeof(string), typeof(NodeS), new PropertyMetadata("\ue867"));

        private FloWTask ParentItemsControl_;
        private bool disposedValue;

        public FloWTask ParentItemsControl
        {
            get { return ParentItemsControl_; }
            set { ParentItemsControl_ = value; }
        }
        /// <summary>
        /// 节点描述信息
        /// </summary>
        public string Description
        {
            get { return (string)GetValue(DescriptionProperty); }
            set { SetValue(DescriptionProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Description.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty DescriptionProperty =
            DependencyProperty.Register("Description", typeof(string), typeof(NodeS), new PropertyMetadata(null));

        protected virtual void Dispose(bool disposing)
        {
            if (!disposedValue)
            {
                if (disposing)
                {
                    // 清理托管资源
                    nodeS?.Dispose(); // 托管资源需要显式释放                }

                    // TODO: 释放未托管的资源(未托管的对象)并重写终结器
                    // TODO: 将大型字段设置为 null
                    disposedValue = true;
                }
            }
        }

        // // TODO: 仅当“Dispose(bool disposing)”拥有用于释放未托管资源的代码时才替代终结器
        // ~NodeS()
        // {
        //     // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
        //     Dispose(disposing: false);
        // }

        public void Dispose()
        {
            // 不要更改此代码。请将清理代码放入“Dispose(bool disposing)”方法中
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }

        // 析构器（如果不显式调用 Dispose，会由垃圾回收器调用）
        ~NodeS()
        {
            // 仅释放非托管资源
            Dispose(disposing: false);
        }
        //public static readonly DependencyProperty ParentItemsControlProperty =
        //  DependencyProperty.Register("ParentItemsControl", typeof(FloWTask), typeof(NodeS), new PropertyMetadata(null));

        //public FloWTask ParentItemsControl
        //{
        //    get => (FloWTask)GetValue(ParentItemsControlProperty);
        //    set => SetValue(ParentItemsControlProperty, value);
        //}
    }
}
